/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.absisgroup.nt;

import com.absisgroup.nsc.utilities.GenericServlet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author Hue
 */
public class LoaiHangHoa extends GenericServlet {

    @Override
    public int process(final Connection con, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
        writeToResponse(response, toJSONObject(con));
        
        return DONT_RUN_JSP;
    }
    
    private String toJSONObject(final Connection con) throws Exception {
        final JSONArray root = new JSONArray();
        String strQuery = "select PRODT_PK, PRODT_NAME from PRODUCT_TYPES order by PRODT_NAME";
        PreparedStatement pStmt = con.prepareStatement(strQuery);
        ResultSet rs = pStmt.executeQuery();
        while(rs.next()) {
            JSONObject jsEntity = new JSONObject();
            jsEntity.put("pk", rs.getString("PRODT_PK"));
            jsEntity.put("name", rs.getString("PRODT_NAME"));
            root.put(jsEntity);
        }
        pStmt.close();

        final JSONObject jsObj = new JSONObject();
        jsObj.put("root", root);
        
        return jsObj.toString();
    }
    
    @Override
    protected boolean useConnection() {
        return true;
    }
}
